package cn.com.ddapp.ddm.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.bean.ClassifyGoodBean;
import cn.com.ddapp.ddm.bean.HomeBean;
import cn.com.ddapp.ddm.util.TDevice;


public class ChaotejiaAdpter extends BaseAdapter {
    private Context mContext;
    private List<HomeBean.Chaodijia> mLists ;
    private LayoutInflater layoutInflater;

    public ChaotejiaAdpter(Context context) {
        mContext = context;
        layoutInflater=LayoutInflater.from(mContext);
    }
    public void updata(List<HomeBean.Chaodijia> lists){
        this.mLists = lists;
       notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return mLists != null ? mLists.size():0;
    }

    @Override
    public Object getItem(int i) {
        return mLists.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    public void addView(List<HomeBean.Chaodijia> ll){
        if(ll.size()<=0){
            return;
        }else{
            mLists.addAll(ll);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder=null;
        if(convertView == null){
            convertView= layoutInflater.inflate(R.layout.landhomeclassifygood_item,null);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
            AutoUtils.auto(convertView);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }

        int width = (int)(TDevice.getScreenWidth()-110)/5;
        int height = (int)(TDevice.getScreenWidth()-90)/3;
        LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(width,width);
        holder.classify_img.setLayoutParams(params2);

        HomeBean.Chaodijia bean=mLists.get(position);
        holder.classify_price.setText("￥ "+String.valueOf(bean.getPriceFlash()));
        holder.classify_preprice.setText("￥ "+String.valueOf(bean.getPrice()));
        holder.classify_preprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//加横线
        holder.classify_name.setText(bean.getName());
        holder.classify_sold.setText("销量  "+String.valueOf(bean.getSales())+" 件");
        String []nUrl=bean.getHeadImg().split("\\.");
        StringBuffer buffer=new StringBuffer();
        buffer.append(nUrl[0]).append("_360.").append(nUrl[1]);
//        if(nUrl[1].equals("jpg")){
//            buffer.append(nUrl[0]).append("_500.jpg");
//        }else if(nUrl[1].equals("png")){
//            buffer.append(nUrl[0]).append("_500.png");
//        }else if(nUrl[1].equals("jpeg")){
//            buffer.append(nUrl[0]).append("_500.jpeg");
//        }
//        buffer.append(nUrl[0]).append("_500.jpg");
        Glide.with(mContext).load(OKUtilAPI.imgBaseUrl+buffer).into(holder.classify_img);

        return convertView;
    }


    static class ViewHolder{
        @BindView(R.id.classify_img)
        ImageView classify_img;
        @BindView(R.id.classify_name)
        TextView classify_name;
        @BindView(R.id.classify_preprice)
        TextView classify_preprice;
        @BindView(R.id.classify_price)
        TextView classify_price;
        @BindView(R.id.classify_kucun)
        TextView classify_kucun;
        @BindView(R.id.classify_sold)
        TextView classify_sold;


        public ViewHolder(View view){
            ButterKnife.bind(this,view);
        }
    }
}
